//
//  CYStatusPictureView.swift
//  CYSinaWB
//
//  Created by mac on 16/2/24.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class CYStatusPictureView: UICollectionView {
    //配置类型的属性
    private let pictureCellId = "pictureCellId"
    let itemWidth = (ScreenWidth - 2 * statusCellMargin - pictureCellMargin * 2) / 3
    //配图视图的数据源数组
    var imageUrls: [NSURL]? {
        didSet {
            //计算配图视图大小
            let pSize = calclatePictureViewSize()
            //更新视图配图大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            //只要数据源发生改变就需要 reloadData
            reloadData()
            testLabel.text = "\(imageUrls?.count ?? 0)"
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth)
        //最小间距
        flowLayout.minimumInteritemSpacing = pictureCellMargin
        flowLayout.minimumLineSpacing = pictureCellMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        self.registerClass(PictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        self.dataSource = self
        self.backgroundColor = UIColor.brownColor()
        addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //根据视图配图数量 计算配图视图真实大小
    private func calclatePictureViewSize() -> CGSize {
        let imageCount = imageUrls?.count ?? 0
        if imageCount == 0 {
            return CGSizeZero
        }
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        if imageCount == 1 {
            let imageSize = CGSize(width: 150, height: 200)
            
            layout.itemSize = imageSize
            
            return imageSize
        }
        //如果不是单张图片 需要重新设置collectiView 的cell的大小  防止cell复用时候出现显示的问题
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        if imageCount == 4 {
            let w = itemWidth * 2 + 2 * pictureCellMargin
            
            return CGSize(width: w, height: w)
        }
        
        /*
        1,2,3   -> 1
        4,5,6   -> 2
        7,8,9   -> 3
        */
        //多张配图
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let w = itemWidth * 3 + 2 * pictureCellMargin
        let h = itemWidth * row + (row - 1) * pictureCellMargin
        
        return CGSizeMake(w, h)
    }
    
    private lazy var  testLabel: UILabel = UILabel(text: "", fontSize: 14, textColor: UIColor.blueColor())
}
extension CYStatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.imageUrls?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! PictureCell
        cell.backgroundColor = UIColor.grayColor()
        
        cell.imageURL = imageUrls![indexPath.item]
        return cell
    }
}

private class PictureCell: UICollectionViewCell {
    
    var imageURL: NSURL? {
        didSet {
            iconView.sd_setImageWithURL(imageURL)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.brownColor()
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        contentView.addSubview(iconView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    
    
    
    private lazy var iconView: UIImageView = {
        let iv = UIImageView()
        //部分填充
        iv.contentMode = .ScaleAspectFill
        iv.clipsToBounds = true
        return iv
        }()
}